package ca.tatham.triplipse.views;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;

import ca.tatham.triplipse.Activator;

public abstract class RdfStatementLabelProvider extends LabelProvider implements
		IRdfStatementLabelProvider {

	private static final char OPEN_REF = '<';
	private static final char CLOSE_REF = '>';
	private static final char OPEN_LITERAL = '"';
	private static final char CLOSE_LITERAL = '"';

	private boolean m_prefix = true;

	public boolean isPrefixing() {
		return m_prefix;
	}

	protected final String getLiteralText(String literal) {
		return OPEN_LITERAL + literal + CLOSE_LITERAL;
	}

	protected final String getResourceText(String resource) {
		if (isPrefixing()) {
			String[] prefix = Activator.getDefault().getRdfDocument()
					.getPrefix(resource);
			if (prefix != null) {
				return prefix[0] + ':' + resource.substring(prefix[1].length());
			}
		}
		return OPEN_REF + resource + CLOSE_REF;
	}

	public final boolean togglePrefix() {
		if (m_prefix == true) {
			m_prefix = false;
		} else {
			m_prefix = true;
		}
		fireLabelProviderChanged(new LabelProviderChangedEvent(this));
		return m_prefix;
	}

}
